﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using TheDarkKnight;
using UnityEditor;
using UnityEngine;
using static TheDarkKnight.TempletObjectBase;

public class SetModelCenterPosition : MonoBehaviour
{
    public TempletObjectBase Templet;

    [Header("中心点位置")]
    public Transform CenterPosition;
    private MeshFilter m_Filter;

    // Start is called before the first frame update
    void Start()
    {
        List<Vector3> verticesList = new List<Vector3>();
        List<Vector2> UVList = new List<Vector2>();
        List<Vector3> normalList = new List<Vector3>();
        List<int> triangleList = new List<int>();
        for (int i = 0; i < Templet.Vectices.Count; i++)
        {
            VerticesData data = Templet.Vectices[i];
            verticesList.Add(data.Vertices);
            UVList.Add(data.UV);
            normalList.Add(data.Normal);
        }
        for (int j = 0; j < Templet.Triangles.Count; j++)
        {
            TriangelData data = Templet.Triangles[j];
            triangleList.Add(data.FirstVertice);
            triangleList.Add(data.SecondVertice);
            triangleList.Add(data.ThridVertice);
        }
        Mesh templetMesh = new Mesh();
        templetMesh.vertices = verticesList.ToArray();
        templetMesh.uv = UVList.ToArray();
        templetMesh.normals = normalList.ToArray();
        templetMesh.triangles = triangleList.ToArray();
        m_Filter = this.gameObject.AddComponent<MeshFilter>();
        m_Filter.mesh = templetMesh;
        MeshRenderer render = this.gameObject.AddComponent<MeshRenderer>();
        render.material = new Material(Shader.Find("Standard"));
    }

    public void AdjustPivot()
    {
        string directoryPath = @"Assets/ScriptableObject/AdjustPivot/";
        string fullDirPath = System.Environment.CurrentDirectory + "/" + directoryPath;
        TempletObjectBase templet = Templet;
        TempletObjectBase data = (TempletObjectBase)ScriptableObject.CreateInstance(templet.TypeName);
        //data.Vectices = templet.Vectices;
        data.TypeName = templet.TypeName;
        data.ModelName = templet.ModelName;
        data.Triangles = templet.Triangles;
        data.SurfaceMaterial = templet.SurfaceMaterial;
        data.SectionMaterial = templet.SectionMaterial;
        data.Vectices = new List<VerticesData>();
        for (int j = 0; j < templet.Vectices.Count; j++)
        {
            VerticesData newVerticeData = new VerticesData();
            newVerticeData.Vertices = templet.Vectices[j].Vertices +(templet.Vectices[j].Vertices - CenterPosition.position) - templet.Vectices[j].Vertices;
            newVerticeData.UV = templet.Vectices[j].UV;
            newVerticeData.Normal = templet.Vectices[j].Normal;
            data.Vectices.Add(newVerticeData);
        }
        if (!Directory.Exists(fullDirPath))
        {
            Directory.CreateDirectory(fullDirPath);
        }
        AssetDatabase.CreateAsset(data, directoryPath + data.TypeName + "_Pivot" + ".asset");
        AssetDatabase.SaveAssets(); //存储资源
        AssetDatabase.Refresh(); //刷新
        Debug.Log("调整顶点位置完毕!");
    }

    // Update is called once per frame
    void Update()
    {

    }

}
